﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.View;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.View
{
    public partial class View_GetPickUpMainBusiness : BaseBusiness<View_GetPickUpMain>, IView_GetPickUpMainBusiness, ITransientDependency
    {
        public View_GetPickUpMainBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<View_GetPickUpMain>> GetDataListAsync(PageInput<GetPickUpMainQueryDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<View_GetPickUpMain>();
            if (!input.Search.ToReviewStatus.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_GetPickUpMain, bool>(
                    ParsingConfig.Default, false, $@"ToReviewStatus=(@0)", input.Search.ToReviewStatus);
                where = where.And(newWhere);
            }

            if (!input.Search.NumbersOrCustomer.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_GetPickUpMain, bool>(
                  ParsingConfig.Default, false, $@"Id.contains(@0)", input.Search.NumbersOrCustomer);
                where = where.And(newWhere);
            }
            if (!input.Search.StyleName.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_GetPickUpMain, bool>(
                  ParsingConfig.Default, false, $@"StyleName.equals(@0)", input.Search.StyleName);
                where = where.And(newWhere);
            }
            if (!input.Search.OrderTimeStart.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_GetPickUpMain, bool>(
                  ParsingConfig.Default, false, $@"CreateTime>(@0)", input.Search.OrderTimeStart);
                where = where.And(newWhere);
            }
            if (!input.Search.OrderTimeEnd.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_GetPickUpMain, bool>(
                  ParsingConfig.Default, false, $@"CreateTime<(@0)", input.Search.OrderTimeEnd);
                where = where.And(newWhere);
            }
            if (!input.Search.Status.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_GetPickUpMain, bool>(
                  ParsingConfig.Default, false, $@"StatusIn=(@0)", input.Search.Status);
                where = where.And(newWhere);
            }
            if (!input.Search.ParentId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_GetPickUpMain, bool>(
                  ParsingConfig.Default, false, $@"ParentId.equals(@0)", input.Search.ParentId);
                where = where.And(newWhere);
            }
           
            if (input.Search.NumbersOrCustomer.IsNullOrEmpty() && input.Search.StyleName.IsNullOrEmpty() && input.Search.OrderTimeStart.IsNullOrEmpty() && input.Search.OrderTimeEnd.IsNullOrEmpty() && input.Search.Status.IsNullOrEmpty() && input.Search.ParentId.IsNullOrEmpty())
            {
                string nowdate = DateTime.Now.ToString("yyyy-MM-dd");
                where = where.And(x => x.DeliveryTime >= Convert.ToDateTime(nowdate) && x.DeliveryTime <= Convert.ToDateTime(nowdate).AddDays(1).AddSeconds(-1));
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<View_GetPickUpMain> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(View_GetPickUpMain data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(View_GetPickUpMain data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<PageResult<View_PickUpMain>> GetDataListMessageAsync(PageInput<GetPickUpMainQueryDTO> input)
        {
            var q = Db.GetIQueryable<View_PickUpMain>();
            var where = LinqHelper.True<View_PickUpMain>();
            where = where.And(m=>m.ToReviewStatus==1);
            if (!input.Search.NumbersOrCustomer.IsNullOrEmpty()) {
                where = where.And(m=>m.Id.Contains(input.Search.NumbersOrCustomer));
            }
            if (!input.Search.OrderTimeStart.IsNullOrEmpty() && !input.Search.OrderTimeEnd.IsNullOrEmpty())
            {
                DateTime OrderTimeStart = Convert.ToDateTime(input.Search.OrderTimeStart);
                DateTime OrderTimeEnd = Convert.ToDateTime(input.Search.OrderTimeEnd);
                where = where.And(x => x.DeliveryTime >= Convert.ToDateTime(OrderTimeStart) && x.DeliveryTime <= Convert.ToDateTime(OrderTimeEnd).AddDays(1).AddSeconds(-1));
            }
            return await q.Where(where).GetPageResultAsync(input);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}